//
//  HomeworkController.swift
//  SchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit
import SVProgressHUD
class HomeworkController: HSBasicController {
    
    let HomeWorkVM = HSHomeWorkViewModal()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置一个高度
        tableView.rowHeight = 100
        //注册圆形cell
        tableView.register(THSHomeWorktableViewCell.self, forCellReuseIdentifier: "homeWorktableViewCell")

        loadData()
    }

    override func loadData() {
        HomeWorkVM.requestHomeData {[weak self] (err, isRefresh) in
            if err != nil {
                return
            }
            if isRefresh {
                self?.tableView.reloadData()
            }
        }
    }
    
    
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "homeWorktableViewCell", for:indexPath) as? THSHomeWorktableViewCell
        
        cell?.selectionStyle = .none
        let model = HomeWorkVM.homeWordArr[indexPath.row]
        //作业标题
        cell?.homeWorkTitleL.text = model.hw_title
        //老师名字
        cell?.teacherL.text = model.hw_tName
        //时间
        cell?.timeL.text = model.hw_createDate
        //科目
        cell?.classNameBtn.setTitle(model.courseName, for: .normal)
        //作业情况
        if HomeWorkVM.homeWordArr[indexPath.row].subStatus == 0 {
            cell?.submiView.image = UIImage(named: "hw_base_commit")
        } else {
            cell?.submiView.image = UIImage(named: "hw_base_no_commit")
        }
        return cell!
    }
    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       
        return HomeWorkVM.homeWordArr.count
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        ///跳转到作业详情界面
        //请求数据
        let url = (baseURLStr ?? "") + "ZH-schoolname-S-getHomeworkCheck"
        
        guard let  key  = userInfo["key"] as?String else {
            return
        }
        guard let hw_id = HomeWorkVM.homeWordArr[indexPath.row].hw_id else{
            return
        }
        
        let parameters = ["key":key,"hw_id":hw_id] as [String : Any]
        
        HSNetWorkTools.shared.downloadData(url: url, parameters: parameters){[weak self](data, error) in
            guard let data = data else{
                SVProgressHUD.showInfo(withStatus: "网络异常，请检查你的网络")
                SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.dark)
                SVProgressHUD.setMinimumDismissTimeInterval(1.0)
                return
            }
            
            //下一级页面数据源数组
            var dataSouceArr:[ClasschildModal] = []
            let arr = ((data as!NSDictionary)["data"])as!NSArray
            for dic in arr{
                let model = ClasschildModal(dic: dic as! [String : Any])
                dataSouceArr.append(model)
            }
            let hwParticularsVC:ClassroomChildViewController =  UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"ClassroomChildViewController") as! ClassroomChildViewController
            
            hwParticularsVC.dataSouce = dataSouceArr
            
            self?.present(hwParticularsVC, animated: true, completion: nil)
            
        }
    }

 

}
